package com.lyl.module.controller;

import cn.hutool.json.JSONObject;
import com.lyl.module.api.client.FileApiClient;
import com.lyl.module.api.service.FileApiService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Mr.Luo
 * @date 2022-07-21 14:19
 * @desc
 */
@RestController
@RequiredArgsConstructor
public class AliFileController {
    private final FileApiService fileApiService;
    private final FileApiClient fileApiClient;

    @GetMapping("/v2/getFileList")
    public JSONObject getFileListV2(String parentFileId){
        return fileApiClient.getFileListV2(parentFileId,100);
    }

    @GetMapping("/v3/getFileList")
    public JSONObject getFileListV3(String parentFileId){
        Map<String, Object> map = new HashMap<>();
        //map.put("drive_id", ALIYUN_GLOBAL_VARIABLE.DRIVE_ID);
        map.put("parent_file_id", parentFileId);
        map.put("limit", 100);
        map.put("order_by", "updated_at");
        map.put("order_direction", "DESC");
        return fileApiClient.getFileListV3(map);
    }

    @GetMapping("/getFileList")
    public JSONObject getFileList(String parentFileId){
        return fileApiService.getFileList(parentFileId);
    }

    @PostMapping("/uploadFile")
    public JSONObject uploadFile(MultipartFile file,String parentFileId) throws IOException {
        return fileApiService.uploadFile(file,parentFileId);
    }

    @PostMapping("/uploadFileText")
    public JSONObject uploadFileText(String fileText,String parentFileId,String fileName) throws IOException {
        return fileApiService.uploadFile(fileText,parentFileId,fileName);
    }
}
